<template>
  <div class="container">
    <div class="app-container">
      <div style="padding: 10px">
        <el-button type="primary" size="mini" @click="addPermissionClick('add', 0)">添加权限</el-button>
      </div>
      <!-- 表格 -->
      <el-table :data="permissionList" row-key="id" default-expand-all>
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="code" label="标识" />
        <el-table-column prop="description" label="描述" />
        <el-table-column label="操作">
          <template #default="{row}">
            <el-button v-if="row.type === 1" type="text" size="mini" @click="addPermissionClick('add',row.id)">添加</el-button>
            <el-button type="text" size="mini" style="margin-right:10px" @click="addPermissionClick('edit',row.id)">编辑</el-button>
            <el-popconfirm title="您确定删除该权限吗？" @onConfirm="delConfirmClick(row.id)">
              <template #reference>
                <el-button type="text" size="mini">删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 添加和编辑权限的弹层 -->
    <el-dialog :title="type==='add'? '新增权限':'编辑权限'" width="500px" :visible.sync="showDialog" @close="close">
      <!-- 权限表单 -->
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item prop="name" label="权限名称">
          <el-input v-model.trim="form.name" size="mini" style="width:300px" />
        </el-form-item>
        <el-form-item prop="code" label="权限标识">
          <el-input v-model.trim="form.code" size="mini" style="width:300px" />
        </el-form-item>
        <el-form-item prop="description" label="权限描述">
          <el-input v-model.trim="form.description" size="mini" style="width:300px" type="textarea" />
        </el-form-item>
        <el-form-item prop="enVisible" label="开启">
          <el-switch
            v-model="form.enVisible"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <el-button type="primary" size="mini" @click="confirm">确定</el-button>
              <el-button size="mini" @click="close">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { addPermission, getPermissionList, getPermissionDetail, updatePermission, delPermission } from '@/api/permission'
import { translateListToTreeData } from '@/utils'
export default {
  data() {
    return {
      // 弹出层标题
      type: '',
      // 表单数据
      form: {
        name: '', // 权限名称
        code: '', // 权限标识
        description: '', // 权限描述
        enVisible: '0', // 权限开启状态："1" 开启 "0" 不开启，默认开启
        type: 1, // 权限类型：1 一级权限 2 二级权限
        pid: 0 // 父级权限ID
      },
      rules: {
        name: [
          { required: true, message: '权限名称不能为空', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '权限标识不能为空', trigger: 'blur' }
        ]
      },
      showDialog: false,
      permissionList: []
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    // 删除事件
    async  delConfirmClick(id) {
      await delPermission(id)
      this.$message.success('删除成功')
      this.getPermissionList()
    },
    // 点击添加编辑弹层事件
    async addPermissionClick(type, id) {
      this.showDialog = true
      if (type === 'add') {
        this.type = 'add'
        // 2级权限事件
        if (id !== 0) {
          this.form.pid = id
          this.form.type = 2
        }
      } else {
        this.type = 'edit'
        this.form = await getPermissionDetail(id)
      }
    },
    // 点击确定事件
    async confirm() {
      // 表单验证
      await this.$refs.form.validate()
      // 判断是什么事件类型
      if (this.type === 'add') {
        await addPermission(this.form)
        this.$message.success('新增权限成功')
      } else {
        await updatePermission(this.form)
        this.$message.success('修改权限成功')
      }
      this.close()
      this.getPermissionList()
    },
    // 关闭弹层事件
    close() {
      this.$refs.form.resetFields()
      this.form = {
        name: '', // 权限名称
        code: '', // 权限标识
        description: '', // 权限描述
        enVisible: '0', // 权限开启状态："1" 开启 "0" 不开启，默认开启
        type: 1, // 权限类型：1 一级权限 2 二级权限
        pid: 0 // 父级权限ID
      }
      this.showDialog = false
    },
    // 初始化列表
    async getPermissionList() {
      const res = await getPermissionList()
      console.log(res)
      this.permissionList = translateListToTreeData(res, 0)
    }
  }
}
</script>

<style>

</style>

